package com.OrderingSystem.service.impl;

import com.OrderingSystem.mapper.ProductInfoMapper;
import com.OrderingSystem.pojo.ProductInfo;
import com.OrderingSystem.service.IProductInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Chenlin
 * @since 2025-08-11
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService {

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Override
    public List<ProductInfo> selectAllProductInfo(Integer pageNum, Integer pageSize, ProductInfo productInfo) {
        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(productInfo.getProductName() != null, "product_name", productInfo.getProductName());
        queryWrapper.between(productInfo.getProductPrice() != null, "product_price", 0, productInfo.getProductPrice());
        queryWrapper.eq(productInfo.getCategoryType() != null, "category_type", productInfo.getCategoryType());
        queryWrapper.eq(productInfo.getProductStatus() != null, "product_status", productInfo.getProductStatus());
        return productInfoMapper.selectList(queryWrapper);
    }

    // 在 ProductInfoServiceImpl 中添加方法
    public List<Map<String, Object>> selectAllProductInfoWithCategoryName(Integer pageNum, Integer pageSize, ProductInfo productInfo) {
        return productInfoMapper.selectProductInfoWithCategoryName(pageNum, pageSize, productInfo);
    }
    
    // 添加获取总数的方法
    public int countAllProductInfoWithCategoryName(ProductInfo productInfo) {
        return productInfoMapper.countProductInfoWithCategoryName(productInfo);
    }

    @Override
    public Integer getTotalProductCount() {
        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
        return productInfoMapper.selectCount(queryWrapper);
    }
}
